package com.mark.shunfeng;

import com.mark.shunfeng.util.HttpClientUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.UUID;

import java.util.Map;

@SpringBootApplication
public class ShunFengApplication {

    // 填入自己的 ID
    private static String PARTNER_ID = "ZFBSv";
    // 请求网址
    private static String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";
    // 现在指定 API的服务名
    private static String SERVICE_CODE = "EXP_RECE_CREATE_ORDER";
    // 请求报文
//    private static String msgData = "{\n\"language\": \"0\", \"trackingType\": \"1\", \"trackingNumber\": [\"444003077898\", \"441003077850\"], \"methodType\": \"1\" }";
    private static String msgData = "{\n" +
            "    \"cargoDetails\":[\n" +
            "        {          \n" +
            "            \"count\":2.365,\n" +
            "\t\t\t\"unit\":\"个\",\n" +
            "\t\t\t\"weight\":6.1,\n" +
            "\t\t\t\"amount\":100.5111,\n" +
            "            \"currency\":\"HKD\",\n" +
            "            \"name\":\"护肤品1\",           \n" +
            "            \"sourceArea\":\"CHN\"          \n" +
            "        }],\n" +
            "    \"contactInfoList\":\t[\n" +
            "        {\n" +
            "            \"address\":\"广东省深圳市南山区软件产业基地11栋\",\n" +
            "            \"contact\":\"小曾\",\n" +
            "            \"contactType\":1,\n" +
            "            \"country\":\"CN\",\n" +
            "            \"postCode\":\"580058\",\n" +
            "            \"tel\":\"4006789888\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"address\":\"广东省广州市白云区湖北大厦\",\n" +
            "            \"company\":\"顺丰速运\",\n" +
            "            \"contact\":\"小邱\",\n" +
            "            \"contactType\":2,\n" +
            "            \"country\":\"CN\",\n" +
            "            \"postCode\":\"580058\",\n" +
            "            \"tel\":\"18688806057\"\n" +
            "        }],\n" +
            "    \"language\":\"zh_CN\",\n" +
            "    \"orderId\":\"OrderNum20200612223\"\n" +
            "}";



    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        SpringApplication.run(ShunFengApplication.class, args);

        // requestID
        String requestID = UUID.randomUUID().toString().replace("-", "");
        System.out.println("唯一订单号："+requestID);
        //客户校验码    使用顺丰分配的客户校验码
        String checkWord = "zMZ5qnPbv5lhdsIZWorJNIkgPbGthDuX";
        //时间戳 取报文中的timestamp（调用接口时间戳）
        String timestamp = String.valueOf(System.currentTimeMillis());
        //业务报文  去报文中的msgData（业务数据报文）

        //将业务报文+时间戳+校验码组合成需加密的字符串(注意顺序)
        String toVerifyText = msgData+timestamp+checkWord;
        System.out.println("时间戳："+timestamp);
        //因业务报文中可能包含加号、空格等特殊字符，需要urlEnCode处理
        toVerifyText = URLEncoder.encode(toVerifyText,"UTF-8");
        //进行Md5加密
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(toVerifyText.getBytes("UTF-8"));
        byte[] md = md5.digest();
        //通过BASE64生成数字签名
        String msgDigest = new String(new BASE64Encoder().encode(md));
        System.out.println("数字签名："+msgDigest);

        Map<String,String> params = new HashMap<>();
        params.put("requestID",requestID);
        params.put("msgDigest",msgDigest);
        params.put("msgData",msgData);
        params.put("timestamp",timestamp);
        params.put("partnerID",PARTNER_ID);
        params.put("serviceCode",SERVICE_CODE);

        String result = null;
        try {
            result = HttpClientUtil.post(CALL_URL_BOX, params);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(result);
    }

}
